/**********************************
 SaveVectorField class
 
Saves the velocity and pressure field to a text file with customizable header
 
import processing.video.*;
int recording=1;
SaveVectorField vel1, vel2, vel3;
AudreyTest test;

void setup(){
  int resolution = 128, xLengths=6, yLengths=3, zoom = 1;
  float xStart = -4, yDist =0.2;
  test = new AudreyTest(resolution, xLengths, yLengths, xStart , yDist, zoom);
  vel1 = new SaveVectorField("vfield_d02_D05_Re10000_t-03linear.txt",test.body.a.coords,test.Re,resolution, test.n,test.m);
  vel2 = new SaveVectorField("vfield_d02_D05_Re10000_t0linear.txt",test.body.a.coords,test.Re,resolution, test.n,test.m);
  vel3 = new SaveVectorField("vfield_d02_D05_Re10000_t06linear.txt",test.body.a.coords,test.Re,resolution, test.n,test.m);
}

void draw(){

  test.update();
  if((test.t>-0.3) && (recording==1)){  
    vel1.addField(test.flow.u,test.flow.p);
    recording = 2;
  }
  if((test.t>0.) && (recording==2)){
    vel2.addField(test.flow.u,test.flow.p);
    recording = 3;
  }
  if((test.t>0.6) && (recording==3)){
    vel3.addField(test.flow.u,test.flow.p);
    vel1.finish();
    vel2.finish();
    vel3.finish();
    exit();
  }
}

void keyPressed(){
    vel1.finish();
    vel2.finish();
    vel3.finish();
    exit();
}
 ***********************************/

class SaveVectorField {
  PrintWriter output;
  int m, n;

  SaveVectorField(String name, ArrayList<PVector> coords, float Re, int resolution, int n, int m) {
    this.m = m;
    this.n = n;
    output = createWriter(name);
    output.println("%% Steady field generated by a NACA foil. u, followed by v, followed by p");
    output.print("% xcoord = [");
    for (int i=0; i<coords.size(); i++) {
      output.print(coords.get(i).x +" ");
    }
    output.println("];");

    output.print("% ycoord = [");
    for (int i=0; i<coords.size(); i++) {
      output.print(coords.get(i).y +" ");
    }
    output.println("];");

    output.print("% resolution = "+ resolution);
    output.print("; n = "+ n);
    output.print("; m = "+ m);
    output.print("; Re = "+ Re);
    output.println(";");
  }


  void addField(VectorField u, Field p) {
    for (int j=1; j<m-1; j++) {
      for (int i=1; i<n-1; i++) {
        output.print(u.x.a[i][j] +" ");
      }
      output.println(";");
    }
    for (int j=1; j<m-1; j++) {
      for (int i=1; i<n-1; i++) {
        output.print(u.y.a[i][j] +" ");
      }
      output.println(";");
    }
    for (int j=1; j<m-1; j++) {
      for (int i=1; i<n-1; i++) {
        output.print(p.a[i][j] +" ");
      }
      output.println(";");
    }
  }

  void finish() {
    output.flush(); // Writes the remaining data to the file
    output.close(); // Closes the file
  }
} 

